﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Enemy : MonoBehaviour
{
    public float hp;
    private float total_hp;
    public GameObject explosion_effect;
    public Slider blood_line; 
    public float move_speed;
    private Transform[] tags;
    private int now_point = 0;
    private void Start()
    {
        tags = GetTags.tags;
        total_hp = hp;
        transform.LookAt(tags[now_point]);
    }
    private void Update()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * move_speed);
        if (Vector3.Distance(transform.position, tags[now_point].position) < 0.5f)
        {
            now_point++;
            if (now_point < tags.Length)
            {
                transform.LookAt(tags[now_point]);
            }
            else
            {
                ReachEnd();
            }
        }
    }

    private void ReachEnd()
    {
        Destroy(this.gameObject);
        GameManager.instance.Lose();
    }

    public void GetHurt(float damage)
    {
        hp -= damage;
        if (hp < 0)
        {
            EnemyDie();
        }
        blood_line.value = (float)hp / total_hp;
    }

    private void EnemyDie()
    {
        EnemySpawner.enemy_sum--;
        Destroy(this.gameObject);
        GameObject go = GameObject.Instantiate(explosion_effect,transform.position,Quaternion.identity);
        Destroy(go,0.5f); //死亡特效
    }
}
